# Generated manually for unified employee model

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

    dependencies = [
        ('users', '0002_administrator_avatar_administrator_bio_and_more'),
    ]

    operations = [
        # 1. 使用 SQL 安全地删除旧表（如果存在）
        migrations.RunSQL(
            sql=[
                # 删除 Administrator 相关的表
                "DROP TABLE IF EXISTS `administrators_groups`;",
                "DROP TABLE IF EXISTS `administrators_user_permissions`;",
                "DROP TABLE IF EXISTS `administrators`;",
                # 删除 SafetyOfficer 表
                "DROP TABLE IF EXISTS `safety_officers`;",
                # 删除旧的 Employee 表（如果存在）
                "DROP TABLE IF EXISTS `employees`;",
            ],
            reverse_sql=[
                # 回滚操作（留空，因为我们无法恢复删除的表）
            ],
        ),
        
        # 2. 创建新的统一Employee模型
        migrations.CreateModel(
            name='Employee',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, verbose_name='用户ID')),
                ('username', models.CharField(help_text='用于登录的用户名', max_length=150, unique=True, verbose_name='用户名')),
                ('password', models.CharField(help_text='加密存储的密码', max_length=128, verbose_name='密码')),
                ('name', models.CharField(max_length=50, verbose_name='姓名')),
                ('gender', models.CharField(blank=True, choices=[('男', '男'), ('女', '女')], max_length=2, verbose_name='性别')),
                ('phone', models.CharField(blank=True, max_length=20, verbose_name='手机号')),
                ('email', models.EmailField(blank=True, max_length=254, verbose_name='邮箱')),
                ('department', models.CharField(blank=True, max_length=50, verbose_name='所属部门')),
                ('position', models.CharField(blank=True, max_length=50, verbose_name='岗位')),
                ('hire_date', models.DateField(blank=True, null=True, verbose_name='入职日期')),
                ('status', models.CharField(choices=[('启用', '启用'), ('停用', '停用')], default='启用', max_length=10, verbose_name='账号状态')),
                ('avatar', models.CharField(blank=True, help_text='头像文件相对路径', max_length=200, verbose_name='头像路径')),
                ('bio', models.TextField(blank=True, help_text='个人简介或备注信息', verbose_name='个人简介')),
                ('remarks', models.TextField(blank=True, verbose_name='备注')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='修改时间')),
                # 角色标识字段
                ('is_admin', models.BooleanField(default=False, verbose_name='是否管理员')),
                ('is_safety_officer', models.BooleanField(default=False, verbose_name='是否安全员')),
                ('is_employee', models.BooleanField(default=True, verbose_name='是否普通员工')),
                # 管理员特有字段
                ('permissions', models.JSONField(blank=True, default=dict, verbose_name='权限列表')),
                ('last_login_time', models.DateTimeField(blank=True, null=True, verbose_name='最后登录时间')),
                # 普通员工特有字段
                ('employee_number', models.CharField(blank=True, help_text='普通员工编号', max_length=20, verbose_name='员工编号')),
                ('contact', models.CharField(blank=True, max_length=20, verbose_name='联系方式')),
                ('years_of_service', models.IntegerField(default=0, verbose_name='从业年限')),
                ('total_tasks', models.IntegerField(default=0, verbose_name='整改任务数')),
                ('completed_tasks', models.IntegerField(default=0, verbose_name='已完成整改数')),
                ('completion_rate', models.FloatField(default=0.0, verbose_name='整改完成率')),
                ('avg_completion_time', models.FloatField(default=0.0, verbose_name='平均整改时长(天)')),
                ('last_remediation_date', models.DateField(blank=True, null=True, verbose_name='最近整改日期')),
                ('evaluation', models.CharField(blank=True, choices=[('优', '优'), ('良', '良'), ('中', '中'), ('差', '差'), ('待评价', '待评价')], default='待评价', max_length=10, verbose_name='考核结果')),
                # 安全员特有字段
                ('safety_number', models.CharField(blank=True, help_text='安全员编号', max_length=20, verbose_name='安全员编号')),
                ('license_number', models.CharField(blank=True, max_length=30, verbose_name='安全资格证号')),
                ('management_scope', models.CharField(blank=True, max_length=100, verbose_name='管理范围')),
                ('inspection_tasks', models.IntegerField(default=0, verbose_name='排查任务数')),
                ('last_inspection_date', models.DateField(blank=True, null=True, verbose_name='最近排查日期')),
            ],
            options={
                'verbose_name': '员工',
                'verbose_name_plural': '员工',
                'db_table': 'employees',
            },
        ),
        
        # 3. 添加索引以提高查询性能
        migrations.AddIndex(
            model_name='employee',
            index=models.Index(fields=['username'], name='employees_usernam_idx'),
        ),
        migrations.AddIndex(
            model_name='employee',
            index=models.Index(fields=['is_admin', 'is_safety_officer', 'is_employee'], name='employees_role_idx'),
        ),
    ]

